//============================================================================
// Name        : hand_region.cpp
// Author      : dfoelber
//============================================================================

// INCLUDES //
#include "hand_region.h"
#include "graphics/graphics.h"
#include "physics/slide_path.h"
#include "render_manager.h"
#include "image_manager.h"
#include <iostream>
//////////////

using namespace std;

/**
* Constructor
**/
HandRegion::HandRegion() : 
	animatingCard(NULL),
	selectedCard(NULL)
{
}

/**
* Destructor
**/
HandRegion::~HandRegion()
{
	for( int i = 0; i < (int)cardQueue.size(); i++ )
	{
		delete cardQueue[i];
	}
}

/**
* Adds a card to the hand region.
* 
* @param card The card to add
* @return Success
**/
/*bool HandRegion::AddCard( Card* card )
{
	if( (int)objects.size() + (int)cardQueue.size() == MAX_HAND_SIZE )
		return false;
	
	cardQueue.insert(cardQueue.begin(), card);
	
	return true;
}*/

/**
* Removes the currently selected card from the region.
* 
* @return Success
**/
bool HandRegion::ActivateCard()
{
	if( (int)objects.size() == 0 )
		return false;
	
	return true;
}

/**
* Notify the region of an event.
* 
* Overridden from Region.
* 
* @param event The event that happened
* @return If the region used the event
**/
bool HandRegion::ProcessEvent( SDL_Event event )
{	
	if( event.type == SDL_MOUSEBUTTONDOWN )
	{
		Object* current = GetObjectByCoordinate( event.button.x, event.button.y );
		
		if( current && !current->GetPath()->IsMoving() )
		{
			if( current == selectedCard )
			{
				selectedCard->GetPath()->SetPosition(0, selectedCard->GetPath()->GetY());
				
				selectedCard = NULL;
				
				puzzleRegion->CancelSelection();
			}
			else
			{
				if( selectedCard )
				{
					selectedCard->GetPath()->SetPosition(0, selectedCard->GetPath()->GetY());
				}
				
				selectedCard = current;
				selectedCard->GetPath()->SetPosition(CARD_SELECT_INDENT, selectedCard->GetPath()->GetY());
				
				Card* card = (Card*)current;
				puzzleRegion->SetSelection(card->GetInfo()->GetCardCost());
			}
		}
	}
	// for now, return will be activate card
	else if( event.key.keysym.sym == SDLK_RETURN && event.type == SDL_KEYDOWN )
	{
		if( selectedCard )
		{
			selectedCard->GetPath()->SetPosition(0, selectedCard->GetPath()->GetY());
			
			// get number of selections
			int selections = puzzleRegion->GetNumberOfSelections();
			if( selections > 0 )
			{
				CardInfo* info = ((Card*)selectedCard)->GetInfo();
				((Card*)selectedCard)->ClearInfo();
				
				int y = selectedCard->GetPath()->GetY();
				RemoveObject( selectedCard );
				
				for( int i = 0; i < (int)objects.size(); i++ )
				{
					if( objects[i]->GetPath()->GetY() > y )
						objects[i]->SetPosition(objects[i]->GetPath()->GetX(),
								objects[i]->GetPath()->GetY() - objects[i]->GetSurface()->GetHeight());
				}
				
				puzzleRegion->ClearSelections();
				
				playerRegion->Discard(info);
				
				GrabCards();
			}
			
			puzzleRegion->CancelSelection();
			
			selectedCard = NULL;
		}
	}
	
	return Region::ProcessEvent( event );
}

/**
* Notifies of time passing.
* 
* Overridden from Region.
* 
* @param The amount of time, in milliseconds, that passed since the last call
**/
void HandRegion::TimePassed( int ms )
{
	Region::TimePassed( ms );
	
	if( animatingCard && !animatingCard->GetPath()->IsMoving() ) // animation done
	{
		int x = animatingCard->GetPath()->GetX();
		int y = animatingCard->GetPath()->GetY();
		int w = animatingCard->GetPath()->GetWidth();
		int h = animatingCard->GetPath()->GetHeight();
		Path* path = new Path();
		path->SetPosition(x, y);
		path->SetSize(w, h);
		animatingCard->SetPath(path);
		
		animatingCard = NULL;
		
		CheckForHover();
	}
	
	if( animatingCard == NULL && (int)cardQueue.size() > 0 )
	{
		animatingCard = cardQueue.back();
		cardQueue.pop_back();
		
		RenderManager::GetManager()->AddObject(animatingCard);
		
		int length = GetBoundary()->GetHeight() - ((int)objects.size() * (animatingCard->GetPath()->GetHeight() + CARD_SPACING));
		SlidePath* path = new SlidePath(SlidePath::SLIDE_UP, length);
		path->SetPosition(0, GetBoundary()->GetY() + GetBoundary()->GetHeight() - animatingCard->GetPath()->GetHeight());
		path->SetSize(animatingCard->GetPath()->GetWidth(), animatingCard->GetPath()->GetHeight());
		animatingCard->SetPath(path);
		
		AddObject(animatingCard);
	}
}

/**
* Renders the objects on the background
* 
* Overridden from Region.
**/
void HandRegion::Render()
{
	Region::Render();
	
	if( hoverObject && !hoverObject->GetPath()->IsMoving() )
	{
		ImageCustomRender* render = new ImageCustomRender();
		render->surface = ImageManager::GetManager()->GetImage(IMAGE_CARD_BACKGROUND_HOVER);
		render->x = hoverObject->GetPath()->GetX();
		render->y = hoverObject->GetPath()->GetY();
		render->width = hoverObject->GetPath()->GetWidth();
		render->height = hoverObject->GetPath()->GetHeight();
		RenderManager::GetManager()->AddCustomRender(render);
	}
}

/**
* Handles the selected card.
* 
* @param index The card index to set selected
**/
void HandRegion::OnCardSelection(int index)
{
}

/**
* Attempts to fill the hand with cards.
**/
void HandRegion::GrabCards()
{
	int num = MAX_HAND_SIZE - (int)objects.size();
	if( num > 0 )
	{
		for( int i = 0; i < num; i++ )
		{
			cardQueue.insert(cardQueue.begin(), playerRegion->PopCard());
		}
	}
}
